package com.dal.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bean.AccountInfo;
import org.bean.TempRewardDTO;
import org.constant.TempRewardConstant;
import org.utils.RemoteUtil;

import com.dal.db.bean.Account;
import com.dal.db.bean.TempReward;
import com.dal.db.cache.AccountCache;
import com.dal.db.cache.TempRewardCache;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * 临时奖励管理器
 * 
 * @author Alan
 *
 */
public class TempRewardManager {

	static final Log log = LogFactory.get(TempRewardManager.class);

	private TempRewardManager() {
	}

	private static TempRewardManager ME = new TempRewardManager();

	public static TempRewardManager me() {
		return ME;
	}

	/**
	 * 主动下发尚未成功领取的奖励
	 * <p>
	 * <b>手机号来源于第三方，游戏服仅做缓存，方法不再适用。</b>手机号与账号的对应关系必须为一对一
	 * 
	 * @param rewardType
	 *            按类型进行清理,
	 *            {@linkplain org.constant.TempRewardConstant#REWARD_TYPE_ALL
	 *            TempRewardConstant.REWARD_TYPE_ALL}为全部清理
	 */
	@Deprecated
	public void flushReward(int rewardType) {
//		boolean totalFlush = rewardType == TempRewardConstant.REWARD_TYPE_ALL;
//		if (totalFlush || rewardType == TempRewardConstant.REWARD_TYPE_DEFAULT) {
//			// 全部加载
//			List<TempReward> list = TempRewardCache.me().getBaseDao().getAll();
//			if (list != null && list.size() > 0) {
//				Iterator<TempReward> iterator = list.iterator();
//				Map<String, TempReward> phoneNumbers = new HashMap<String, TempReward>(
//						list.size());
//				TempReward tempReward;
//				// 移除已经领取过的记录
//				while (iterator.hasNext()) {
//					tempReward = iterator.next();
//					if (tempReward.isReceive())
//						iterator.remove();
//					else
//						phoneNumbers.put(tempReward.getPhoneNumber(),
//								tempReward);
//				}
//				// 通过手机号向登录服获取uid
//				Set<AccountInfo> infos = RemoteUtil.loginSync()
//						.loadAccountInfoByPhoneNumber(phoneNumbers.keySet());
//				if (infos == null || infos.size() <= 0)
//					return;
//				List<Entry> dtos = new ArrayList<Entry>(infos.size());
//				for (AccountInfo info : infos) {
//					dtos.add(createTempRewardDTO(
//							phoneNumbers.get(info.getPhoneNumber()),
//							info.getUid(),
//							TempRewardConstant.REWARD_TYPE_DEFAULT,
//							info.getGameServerId()));
//				}
//				noticeGameReward(dtos);
//			}
//		}
	}

	public List<TempRewardDTO> getReward(Account account) {
		// 返回list便于其他线下奖励的扩展
		List<TempRewardDTO> dtos = new ArrayList<TempRewardDTO>();
		if (account.getPhoneNumber() != null && !"".equals(account.getPhoneNumber())) {
			TempReward reward = TempRewardCache.me().getByPhoneNumber(account.getPhoneNumber());
			// 判断领取状态
			if (reward!=null && !reward.isReceive()) {
				// default tempreward
				dtos.add(createTempRewardDTO(reward, account.getId(),TempRewardConstant.REWARD_TYPE_DEFAULT,account.getGameServerId()));
			}
		}
		return dtos;
	}

	public void noticeHandleReward(String uid) {
		Account account = AccountCache.me().getById(uid);
		if (account.getPhoneNumber() != null && !"".equals(account.getPhoneNumber())) {
			TempReward reward = TempRewardCache.me().getByPhoneNumber(account.getPhoneNumber());
			// 修改状态
			reward.setReceive(true);
			reward.setReceiveDate(new Date());
			// 保存
			TempRewardCache.me().update(reward);
		}
	}

	private TempRewardDTO createTempRewardDTO(TempReward reward, String uid, int type,String serverId) {
		TempRewardDTO dto = new TempRewardDTO();
		dto.setUid(uid);
		dto.setCreateDate(reward.getCreateDate());
		dto.setRewardType(type);
		HashMap<Integer, Integer> items = new HashMap<Integer, Integer>();
		items.put(reward.getItemId(), reward.getItemCount());
		dto.setItems(items);
		return dto;
	}
}
